package com.uchedao.seller.ui.car.publish;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;

import com.google.gson.Gson;
import com.uchedao.seller.R;
import com.uchedao.seller.config.GlobalInfo;
import com.uchedao.seller.ui.car.base.BasePublishActivity;
import com.uchedao.seller.ui.car.manage.CarForSaleActivity;

/**
 * 车况确认
 */
public class CarPublishActivity extends BasePublishActivity implements View.OnClickListener {

    private CheckBox cb1, cb2, cb3;

    @Override
    protected String getActivityTag() {
        return "CarPublishActivity";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_car_publish;
    }

    @Override
    protected void initView() {
        initToBar("车况确认");
        btnTopRight1.setVisibility(View.VISIBLE);
        btnTopRight1.setText(R.string.btn_for_sale);

        cb1 = (CheckBox) findViewById(R.id.cbCarPublish1);
        cb2 = (CheckBox) findViewById(R.id.cbCarPublish2);
        cb3 = (CheckBox) findViewById(R.id.cbCarPublish3);
    }

    @Override
    protected void initData() {
        if(GlobalInfo.dbHelper.drafHasRecord(GlobalInfo.tempOwner)) {
            new AlertDialog.Builder(this).setMessage("您目前有一辆车在草稿箱中，是否继续发车？")
                    .setOnKeyListener(new DialogInterface.OnKeyListener() {
                        @Override
                        public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                            return true;
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            GlobalInfo.dbHelper.deleteDraf(GlobalInfo.tempOwner);
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            startPublish();
                            finish();
                        }
                    })
                    .show();
        }
    }

    @Override
    protected void initListener() {
        btnTopback.setOnClickListener(this);
        btnTopRight1.setOnClickListener(this);
        findViewById(R.id.btnCarPublish).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.btnTopBack:
                back();
                break;

            case R.id.btnCarPublish:
                if(!cb1.isChecked() || !cb2.isChecked() || !cb3.isChecked()) {
                    new AlertDialog.Builder(this).setMessage("您发布的车辆可能不符合优车到销售标准，无法发布。请再次确认是否符合以下标准，如确认，请勾选‘符合’选项。")
                            .setPositiveButton("确定", null)
                            .show();
                } else {
                    startPublish();
                }
                break;

            case R.id.btnTopRight1:
                // 打开待售车辆列表
                startActivity(new Intent(this, CarForSaleActivity.class));
                break;
        }
    }

    /**
     * 开始发布
     */
    public void startPublish() {
        GlobalInfo.dbHelper.newDraftRecord(GlobalInfo.tempOwner);
        GlobalInfo.draft = GlobalInfo.dbHelper.getDraft(GlobalInfo.tempOwner);
        startActivity(new Intent(this, PublishVehicleActivity.class));
    }

    @Override
    public void onBackPressed() {
        back();
    }

    public void back() {
        new AlertDialog.Builder(this).setMessage("是否放弃发车")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                })
                .show();
    }

    /**
     * 不导出无Expose注解的字段
     */
    public String toJson(Object src) {
        return new Gson().toJson(src);
    }
}
